package com.example.im.controller;

import com.example.im.netty.WebSocketServerHandler;
import com.example.im.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;


@Slf4j
@RestController
@RequestMapping("/api/groups")
public class GroupController {

    @Autowired
    private GroupService groupService;

    private Logger logger = Logger.getLogger(GroupController.class.getName());

    @GetMapping("/counts")
    public int getGroupCount(@RequestParam String user) {
        return groupService.countsInMember(user);
    }
    @GetMapping
    public Set<String> getAllGroups(@RequestParam String user) {
        Set<String> allGroupIds = new LinkedHashSet<>();
        allGroupIds.addAll(groupService.getGroupsByUser(user));
        logger.info(String.valueOf(allGroupIds));
        return allGroupIds;
    }
    @GetMapping("/users")
    public Set<String> getGroupsUsers(@RequestParam String groupId) {
        String users=groupService.getUsersExisted(groupId);
        List<String> userInList=groupService.convertToList(users);
        Set<String> groupUsers = new LinkedHashSet<>(userInList);
        return groupUsers;

    }

    /*public List<String> getGroupsByUser(String userId){
        return getGroupsByUser();
    }*/
} 